<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
      class="search-form"
    >
      <el-form-item label="套餐名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入套餐名称"
          prefix-icon="el-icon-goods"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="套餐价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入套餐价格"
          prefix-icon="el-icon-money"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="流量限制" prop="dataLimit">
        <el-input
          v-model="queryParams.dataLimit"
          placeholder="请输入流量限制"
          prefix-icon="el-icon-upload"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <template slot="append">GB</template>
        </el-input>
      </el-form-item>
      <el-form-item label="通话时长" prop="callMinutes">
        <el-input
          v-model="queryParams.callMinutes"
          placeholder="请输入通话时长"
          prefix-icon="el-icon-phone"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <template slot="append">分钟</template>
        </el-input>
      </el-form-item>
      <el-form-item label="计费周期" prop="billingCycle">
        <el-select
          v-model="queryParams.billingCycle"
          placeholder="请选择计费周期"
          clearable
        >
          <el-option label="月付" value="0" />
          <el-option label="季付" value="1" />
          <el-option label="年付" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="有效期" prop="validityPeriod">
        <el-input
          v-model="queryParams.validityPeriod"
          placeholder="请输入有效期"
          prefix-icon="el-icon-time"
          clearable
          @keyup.enter.native="handleQuery"
        >
          <template slot="append">天</template>
        </el-input>
      </el-form-item>
      <el-form-item label="生效日期" prop="createdAt">
        <el-date-picker
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择生效日期"
          prefix-icon="el-icon-date"
          clearable
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="运营商" prop="operatorId">
        <el-select
          v-model="queryParams.operatorId"
          placeholder="请选择运营商"
          clearable
        >
          <el-option label="中国移动" value="1" />
          <el-option label="中国联通" value="2" />
          <el-option label="中国电信" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:plan:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:plan:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:plan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:plan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="套餐编号" align="center" prop="planId" />-->
      <el-table-column label="套餐名称" header-align="center" align="center" prop="planName">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-s-goods"></i> 套餐名称
          </span>
        </template>
      </el-table-column>
      <el-table-column label="套餐描述" header-align="center" align="center" prop="description">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-document"></i> 套餐描述
          </span>
        </template>
      </el-table-column>
      <el-table-column label="套餐价格(￥)" header-align="center" align="center" prop="price">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-money"></i> 套餐价格(￥)
          </span>
        </template>
      </el-table-column>
      <el-table-column label="流量限制(G)" header-align="center" align="center" prop="dataLimit">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-upload"></i> 流量限制(G)
          </span>
        </template>
      </el-table-column>
      <el-table-column label="通话时长(分钟)" header-align="center" align="center" prop="callMinutes">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-phone"></i> 通话时长(分钟)
          </span>
        </template>
      </el-table-column>
      <el-table-column label="短信条数限制(条)" header-align="center" align="center" prop="smsLimit">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-message"></i> 短信条数限制(条)
          </span>
        </template>
      </el-table-column>
      <el-table-column label="计费周期" header-align="center" align="center" prop="billingCycle">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-time"></i> 计费周期
          </span>
        </template>
        <template slot-scope="scope">
          <span>{{ scope.row.billingCycle === '0' ? '月付' : scope.row.billingCycle === '1' ? '季付' : '年付' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期" header-align="center" align="center" prop="validityPeriod">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-date"></i> 有效期
          </span>
        </template>
      </el-table-column>
<!--      <el-table-column label="状态" align="center" prop="status" />-->
      <el-table-column label="创建时间" header-align="center" align="center" prop="createdAt" width="180">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-timer"></i> 创建时间
          </span>
        </template>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" header-align="center" align="center" prop="updatedAt" width="180">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-refresh"></i> 更新时间
          </span>
        </template>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运营商" header-align="center" align="center" prop="operatorId">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-office-building"></i> 运营商
          </span>
        </template>
        <template slot-scope="scope">
          <span>{{ scope.row.operatorId === 1 ? '中国移动' : scope.row.operatorId === 2 ? '中国联通' : '中国电信' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" header-align="center" align="center" class-name="small-padding fixed-width">
        <template slot="header">
          <span class="custom-header">
            <i class="el-icon-setting"></i> 操作
          </span>
        </template>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:plan:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:plan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改移动套餐管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="套餐名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入套餐名称" prefix-icon="el-icon-goods" />
        </el-form-item>
        <el-form-item label="套餐描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入套餐描述" :rows="3" />
        </el-form-item>
        <el-form-item label="套餐价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入套餐价格" prefix-icon="el-icon-money">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="流量限制" prop="dataLimit">
          <el-input v-model="form.dataLimit" placeholder="请输入流量限制" prefix-icon="el-icon-upload">
            <template slot="append">GB</template>
          </el-input>
        </el-form-item>
        <el-form-item label="通话时长" prop="callMinutes">
          <el-input v-model="form.callMinutes" placeholder="请输入通话时长" prefix-icon="el-icon-phone">
            <template slot="append">分钟</template>
          </el-input>
        </el-form-item>
        <el-form-item label="短信条数" prop="smsLimit">
          <el-input v-model="form.smsLimit" placeholder="请输入短信条数" prefix-icon="el-icon-message">
            <template slot="append">条</template>
          </el-input>
        </el-form-item>
        <el-form-item label="计费周期" prop="billingCycle">
          <el-select v-model="form.billingCycle" placeholder="请选择计费周期" style="width: 100%">
            <el-option label="月付" value="0" />
            <el-option label="季付" value="1" />
            <el-option label="年付" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="有效期" prop="validityPeriod">
          <el-input v-model="form.validityPeriod" placeholder="请输入有效期" prefix-icon="el-icon-time">
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="运营商" prop="operatorId">
          <el-select v-model="form.operatorId" placeholder="请选择运营商" style="width: 100%">
            <el-option label="中国移动" :value="1" />
            <el-option label="中国联通" :value="2" />
            <el-option label="中国电信" :value="3" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan } from "@/api/plan/plan";

export default {
  name: "Plan",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 移动套餐管理表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planName: null,
        description: null,
        price: null,
        dataLimit: null,
        callMinutes: null,
        smsLimit: null,
        billingCycle: null,
        validityPeriod: null,
        status: null,
        createdAt: null,
        updatedAt: null,
        operatorId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询移动套餐管理列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        planName: null,
        description: null,
        price: null,
        dataLimit: null,
        callMinutes: null,
        smsLimit: null,
        billingCycle: null,
        validityPeriod: null,
        status: null,
        createdAt: null,
        updatedAt: null,
        operatorId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加移动套餐管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids
      getPlan(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改移动套餐管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            updatePlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal.confirm('是否确认删除移动套餐管理编号为"' + planIds + '"的数据项？').then(function() {
        return delPlan(planIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
